package com.wen.controller;

import com.wen.entity.po.AppUser;
import com.wen.entity.query.AppUserQuery;
import com.wen.entity.vo.PaginationResultVO;
import com.wen.entity.vo.ResponseVO;
import com.wen.service.AppUserService;
import com.wen.service.impl.UserBatchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 *  Controller
 */
@Slf4j
@RestController("appUserController")
@RequestMapping("/appUser")
public class AppUserController extends ABaseController{

	@Resource
	private AppUserService appUserService;
	/**
	 * 根据条件分页查询
	 */
	@RequestMapping("/list")
	public ResponseVO list(AppUserQuery query){
		log.info("普通分页开始查询");
		long start = System.currentTimeMillis();
		Integer totalCount = appUserService.getTotalCount();
		Integer pageSize = 20000;
		Integer pageTotal = (totalCount + pageSize - 1) / pageSize;
		query.setPageSize(pageSize);
		HashMap<Integer, List<AppUser>> map = new HashMap<>();
		List<AppUser> list = null;
		Long curSum = 0L;
		for(int i=1;i<=pageTotal;i++){
			query.setPageNo(i);
			list = appUserService.getListByPage(query);
			if (!list.isEmpty()){
//				map.put(i, list);
			}
			curSum += list.size();
			log.info("当前查询第{}页，当前页有{}条，总共查询{}",i,list.size(),curSum);
		}
		long end = System.currentTimeMillis();
		log.info("耗时:{}秒",(end-start)/1000);
		int dataNUm = 0;
		for(Integer i=1;i<= map.size();i++)
			dataNUm += map.get(i).size();
//		log.info("map的大小：{},每组数据数量:{},数据总数量:{}条",map.size(),map.get(1).size(),dataNUm);
		return getSuccessResponseVO(map);
	}

	@RequestMapping("/filter-list")
	public ResponseVO filterList(AppUserQuery query){
		log.info("游标分页开始查询");
		long start = System.currentTimeMillis();
		Integer totalCount = appUserService.getTotalCount();
		Integer pageSize = 20000;
		Integer pageTotal = (totalCount + pageSize - 1) / pageSize;
		query.setPageSize(pageSize);
		HashMap<Integer, List<AppUser>> map = new HashMap<>();
		List<AppUser> list = null;
		Long maxId = 0L;
		Long curSum = 0L;
		for(int i=1;i<=pageTotal;i++){
			query.setMaxId(maxId);
			query.setPageNo(i);

			list = appUserService.getListByPage(query);

			if (!list.isEmpty()){
				AppUser appUser = list.get(list.size() - 1);
				maxId =appUser.getId();
//				map.put(i, list);
				curSum += list.size();
			}
			log.info("当前查询第{}页，当前页有{}条，总共查询{}",i,list.size(),curSum);
		}
		long end = System.currentTimeMillis();
		log.info("耗时:{}秒",(end-start)/1000);
		int dataNUm = 0;
		for(Integer i=1;i<= map.size();i++){
			if (map.size()>0) {
				dataNUm += map.get(i).size();
			}
		}
//		log.info("map的大小：{},每组数据数量:{},数据总数量:{}条",map.size(),map.get(1).size(),dataNUm);
		return getSuccessResponseVO(map);
	}
	/**
	 * 根据条件分页查询
	 */
	@RequestMapping("/loadDataList")
	public ResponseVO loadDataList(AppUserQuery query){
		query.setPageSize(30);
		query.setPageNo(2);
		PaginationResultVO<AppUser> resultVO = appUserService.findListByPage(query);
		resultVO.setPageTotal(resultVO.getPageTotal());
		return getSuccessResponseVO(resultVO);
	}
	@Resource
	private UserBatchService userBatchService;

	@RequestMapping("/listByCompletableFuture")
	public ResponseVO listByCompletableFuture(AppUserQuery query){
		List<AppUser> list = userBatchService.query100wUsers();
		log.info("数据总条数:{}",list.size());
		return getSuccessResponseVO(list);
	}

	/**
	 * 新增
	 */
	@RequestMapping("/add")
	public ResponseVO add(AppUser bean) {
		appUserService.add(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增
	 */
	@RequestMapping("/addBatch")
	public ResponseVO addBatch(@RequestBody List<AppUser> listBean) {
		appUserService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增/修改
	 */
	@RequestMapping("/addOrUpdateBatch")
	public ResponseVO addOrUpdateBatch(@RequestBody List<AppUser> listBean) {
		appUserService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据Id查询对象
	 */
	@RequestMapping("/getAppUserById")
	public ResponseVO getAppUserById(Long id) {
		return getSuccessResponseVO(appUserService.getAppUserById(id));
	}

	/**
	 * 根据Id修改对象
	 */
	@RequestMapping("/updateAppUserById")
	public ResponseVO updateAppUserById(AppUser bean,Long id) {
		appUserService.updateAppUserById(bean,id);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据Id删除
	 */
	@RequestMapping("/deleteAppUserById")
	public ResponseVO deleteAppUserById(Long id) {
		appUserService.deleteAppUserById(id);
		return getSuccessResponseVO(null);
	}
}